package game;

import java.util.Scanner;
import java.util.*;
import java.util.logging.*;

public class Game {
    private Room currentRoom;
    private HashMap<String,Handler>handlers=new HashMap<String,Handler>();
    public Game() 
    {
		handlers.put("go",new HanderGo(this));
		handlers.put("bey",new HandlerBey(this));
		handlers.put("help",new HanderHelp(this));
        createRooms();
    }

    private void createRooms()
    {
        Room outside, lobby, pub, study, bedroom,bathroom,fight;
      
        //	制造房间
        outside = new Room("城堡外");
        lobby = new Room("大堂");
        pub = new Room("小酒吧");
        study = new Room("书房");
        bedroom = new Room("卧室");
        bathroom=new Room("浴室");
		fight=new Room("练武场");
        //	初始化房间的出口
        outside.setExits("北",lobby);
        lobby.setExits("南",outside);
        outside.setExits("西",pub);
		pub.setExits("东",outside);
		lobby.setExits("西",study);
		study.setExits("东",lobby);
		lobby.setExits("东",bedroom);
		bedroom.setExits("西",lobby);
		lobby.setExits("上",bathroom);
        bathroom.setExits("下",lobby);
		lobby.setExits("北",fight);
		fight.setExits("南",lobby);

        currentRoom = outside;  //	从城堡门外开始
    }

    private void printWelcome() {
        System.out.println();
        System.out.println("欢迎来到城堡！");
        System.out.println("这是一个超级无聊的游戏。");
        System.out.println("如果需要帮助，请输入 'help' 。");
        System.out.println();
        showPrompt();
    }
	private void showPrompt()
	{
		System.out.println("现在你在" + currentRoom);
        System.out.println(currentRoom.getExitDesc());
        System.out.println();
	}

    // 以下为用户命令
	private void play()
	{
		Scanner in = new Scanner(System.in);
		while ( true ) {
			String line = in.nextLine();
			String[] words = line.split(" ");
			Handler handler=handlers.get(words[0]);
		    String value="";
			if(words.length>1)
				value=words[1];
				if(handler!=null)
				{
					handler.DoCmd(value);
					if(handler.isBey())
						break;
				}
        }
		in.close();
	}

    

    protected void goRoom(String direction) 
    {
        Room nextRoom=currentRoom.getEixt(direction);

        if (nextRoom == null) {
            System.out.println("那里没有门！");
        }
        else {
            currentRoom = nextRoom;
            showPrompt();
        }
    }
	
	public static void main(String[] args) {
		
		Game game = new Game();
		game.printWelcome();
		game.play();
	}

}
